<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <input type="text" id="txt">
  <button id="check">判断是否是图片路径</button>
  <script>
    //   5.var fileName = "abc123.jpg"; 判断图片路径是否符合规则
    // //判断上传头像的路径是否正确：jpg  gif  png  bmp
    // //判断最后一个.出现的索引位置
    var txt = document.getElementById("txt")
    var check = document.getElementById("check");
    check.onclick = function () {
      var txtVal = txt.value; //输入框的值
      var index = txtVal.lastIndexOf("."); //最后一个.出现的位置
      var str = txtVal.substr(index + 1)
      if (str == "jpg" || str == "gif" || str == "png" || str == "bmp") {
        alert("是图片路径")
      } else {
        alert("不是图片路径")
      }
    }

  </script>
</body>

</html>